package lesson_five;

/**
 * @author LvHengChang
 * version 1.0
 * @description:
 */
public class SLList<Object> {
    // 以哨兵节点作为第一个节点
    private StuffNode first;
    private int size;

    public SLList() {
        first = new StuffNode(63, null);
        size = 0;
    }

    public SLList(Object x) {
        first = new StuffNode(63, null);
        first.next = new StuffNode(x, null);
        size += 1;
    }

    public static void main(String[] args) {
        SLList L = new SLList();
        L.addFirst(10);
        L.addFirst(5);
        L.addLast(20);
        System.out.println(L.getFirst());
        System.out.println(L.size());

    }

    public void addFirst(Object x) {
        first.next = new StuffNode(x, first.next);
        size += 1;
    }

    public Object getFirst() {
        return (Object) first.next.item;
    }

    public int size() {
        return size;
    }

    public void addLast(Object x) {
        size += 1;
        StuffNode p = first;
        while (p.next != null) {
            p = p.next;
        }
        p.next = new StuffNode(x, null);
    }

    private class StuffNode<Object> {
        public Object item;
        public StuffNode<Object> next;

        public StuffNode(Object i, StuffNode n) {
            item = i;
            next = n;
        }
    }


}
